package j_util.data_structures.stack;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * This class provides a model of a StackInterface;
 *
 * @author Jose Martin Rozanec;
 */
public class StackE implements StackInterface {
    private int top;
    private Object data[];

    public StackE() {
        data = new Object[10];
        top = -1;
    }

    /**
     * This method allows to know if StackInterface is empty;
     *
     * @return boolean - true if empty, false if not;
     */
    public boolean isEmpty() {
        return (top == -1);
    }

    /**
     * This method allows to see the top Object in a StackInterface;
     *
     * @return top - Object;
     */
    public Object top() {
        return data[top];
    }

    /**
     * This method takes out the las element in a StackInterface;
     */
    public void pop() {
        top--;
    }

    /**
     * This method allows pushing a new element into StackInterface;
     *
     * @param o - Object;
     */
    public void push(Object o) {
        if (top == data.length - 1) {
            resizeArray();
        }
        top++;
        data[top] = o;
    }

    /**
     * This method allows to resize array;
     */
    private void resizeArray() {
        Object b[] = new Object[data.length + 10];
        for (int i = 0; i < data.length; i++) {
            b[i] = data[i];
        }
        data = b;
    }
}

